package com.example.formrememberme;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;

import javax.sql.DataSource;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("cxyxj")
                .password("123").roles("admin", "user")
                .and()
                .withUser("security")
                .password("security").roles("user");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()  //开启配置
                .anyRequest() //其他请求
                .authenticated();//验证   表示其他请求只需要登录就能访问
        http.formLogin();

        http.rememberMe().key("rememberMe-cxyxj").rememberMeParameter("c-r").rememberMeCookieName("cxyxj");

        http.csrf().disable();
    }

    @Autowired
    DataSource dataSource;

    @Bean
    JdbcTokenRepositoryImpl jdbcTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        // 配置数据源
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }
/*   @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()  //开启配置
                .anyRequest() //其他请求
                .authenticated();//验证   表示其他请求只需要登录就能访问
        http.formLogin();

        http.rememberMe().key("rememberMe-cxyxj").tokenRepository(jdbcTokenRepository())
        ;

        http.csrf().disable();
    }*/
}
